package com.wanghang89.gmail.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.wanghang89.gmail.util.db.AdministraRegionDbUtils;

import opensource.jpinyin.PinyinFormat;
import opensource.jpinyin.PinyinHelper;

public class ChineseSpellingUtils {
	
	public static String getSimpleSpelling(String spelling)
	{
		if (spelling==null) {
			return "";
		}
		String[] ss = spelling.split(" ");
		int length = ss.length;
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < length; i++) {
			String s = ss[i];
			if (s!=null&&!s.isEmpty()) {
				builder.append(s.charAt(0));
			}
		}
		return builder.toString();
	}
	
	public static String convertToChineseSpelling(String ch)
	{
		return PinyinHelper.convertToPinyinString(ch," ",PinyinFormat.WITHOUT_TONE);
	}
	
	public static void main(String[] args) {
		
		AdministraRegionDbUtils arDbUtils = (AdministraRegionDbUtils)BDCSContext.getBean("arDbUtils");
		int fromId = 0;
		Map<Integer,String> map = arDbUtils.getNoSpellingAdministraRegions(0);
		while (map!=null) {
			int size = map.size();
			List<Object[]> list = new ArrayList<Object[]>(size);
			Iterator<Integer> ki = map.keySet().iterator();
			while (ki.hasNext()) {
				fromId = ki.next();
				String ch = map.get(fromId);
				String fullSpelling = convertToChineseSpelling(ch);
				String simpleSpelling = getSimpleSpelling(fullSpelling);
				list.add(new Object[]{fullSpelling,simpleSpelling,fromId});
			}
			arDbUtils.updateChineseSpelling(list);
			list.clear();
			list = null;
			map = arDbUtils.getNoSpellingAdministraRegions(fromId);
		}
		
	}
}
